package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysGoodsService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 货物Controller
 * 
 * @author ruoyi
 * @date 2021-05-18
 */
@Controller
@RequestMapping("/system/goods")
public class SysGoodsController extends BaseController
{
    private String prefix = "system/goods";

    @Autowired
    private ISysGoodsService sysGoodsService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("system:goods:view")
    @GetMapping()
    public String goods()
    {
        return prefix + "/goods";
    }


    /**
     * 历史价格
     */
    @GetMapping("/historyMoney/{id}")
    public String historyMoney(@PathVariable("id") Long id, ModelMap mmap) {

        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(id);

        mmap.put("sysGoods", sysGoods);
        return prefix + "/historyMoney";
    }

    /**
     * 查询货物列表
     */
    @RequiresPermissions("system:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysGoods sysGoods, ModelMap map)
    {
        startPage();
        List<SysGoods> list = null;
        Long userId = ShiroUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);

        if (userId == 1) {
            startPage();
            list = sysGoodsService.selectSysGoodsList(sysGoods);
        } else {
            SysDept sysDept = sysDeptService.selectDeptById(sysUser.getDeptId());
            Long parentId = sysDept.getParentId();

            parentId = parentId - 1;

            sysGoods.setPcom(parentId);
            startPage();
            list = sysGoodsService.selectSysGoodsList(sysGoods);
        }
        map.addAttribute("list",list);
        return getDataTable(list);
    }

    /**
     * 导出货物列表
     */
    @RequiresPermissions("system:goods:export")
    @Log(title = "货物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysGoods sysGoods)
    {
        List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
        ExcelUtil<SysGoods> util = new ExcelUtil<SysGoods>(SysGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增货物
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Long userId = ShiroUtils.getUserId();
        mmap.put("userId",userId);
        return prefix + "/add";
    }

    /**
     * 新增保存货物
     */
    @RequiresPermissions("system:goods:add")
    @Log(title = "货物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysGoods sysGoods)
    {
        Long userId = ShiroUtils.getUserId();

        if (userId != 1) {
            SysUser sysUser = sysUserService.selectUserById(userId);
            SysDept sysDept = sysDeptService.selectDeptById(sysUser.getDeptId());
            sysGoods.setCom(sysUser.getDeptId());
            sysGoods.setPcom(sysDept.getParentId());
        }
        return toAjax(sysGoodsService.insertSysGoods(sysGoods));
    }

    /**
     * 修改货物
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(id);
        mmap.put("sysGoods", sysGoods);
        return prefix + "/edit";
    }


    /**
     * 修改保存货物
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "货物", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysGoods sysGoods)
    {
        return toAjax(sysGoodsService.updateSysGoods(sysGoods));
    }

    /**
     * 删除货物
     */
    @RequiresPermissions("system:goods:remove")
    @Log(title = "货物", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysGoodsService.deleteSysGoodsByIds(ids));
    }
}
